<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="inputlib" uri="/WEB-INF/taglibs-input.tld"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="tau.dimes.web.UserFiles"%>
<%@ page import="java.util.List"%>
<%@ page import="org.apache.tomcat.util.http.fileupload.*"%>
<%@ page import="java.io.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Dimes - My Files</title>
</head>
<body>
<%@ include file="Header.jsp"%>
<script type="text/javascript" src="<%=root%>/script/myfiles.js"></script>
<%
	// Handle upload
	String contentType = request.getContentType();
	if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
	{
		DiskFileUpload fu = new DiskFileUpload();
		List<?> files = fu.parseRequest(request);
		FileItem fi = (FileItem) files.iterator().next();
		if (fi.getSize() > 0 && fi.getContentType().equalsIgnoreCase("text/xml"))
		{
			UserFiles userFiles = new UserFiles(login.getUsername(), getServletContext());
			String filename = fi.getName();
			filename = new File(filename).getName();
			if (filename.endsWith(".trace.xml"))
			{
				File file = userFiles.getFile(filename);
				fi.write(file);
			}
		}
	}
%>
<select name="lstFiles" id="lstFiles" size="5">
	<%
		if (login.isLoggedIn())
		{
			UserFiles userFiles = new UserFiles(login.getUsername(), application);
			String[] files = userFiles.listFiles(".trace.xml");
			for (String file : files)
			{
	%>
	<option value="<%=file%>"><%=file.substring(0, file.length() - 10)%></option>
	<%
		}
		}
	%>
</select>
<button id='btnDownload'>Download</button>
<br />
<form id="frmUpload" method="post" enctype="multipart/form-data">
	<input type="file" id="uploadFile" name="uploadFile" /> 
	<input type="submit" id="btnUpload" value="Upload" />
</form>
<iframe id="ifrDownload" src=""	style="display: none; visibility: hidden;"></iframe>
</body>
</html>